<?php
/**
 * Created by PhpStorm.
 * User: 1
 * Date: 2016-11-02
 * Time: 18:07
 */
namespace app\src\order\action;


use app\src\base\action\BaseAction;
use app\src\order\logic\IntentionalOrderLogic;

class IntentionalOrderQueryAction extends BaseAction
{

    /**
     * 搜索
     * @author hebidu <email:346551990@qq.com>
     * @param $uid
     * @param $page_index
     * @param $page_size
     * @return array
     */
    public function query($uid,$page_index,$page_size){

        $logic  = new IntentionalOrderLogic();

        $result = $logic->queryWithCount(['uid'=>$uid],['curpage'=>$page_index,'size'=>$page_size],'update_time desc');

        if($result['status'] && is_array($result['info']['list'])){

            foreach ($result['info']['list'] as &$one){
                $one['order_items'] = json_decode($one['order_items'],JSON_OBJECT_AS_ARRAY);
                $one['processed_order_items'] = $this->preprocess($one['order_items']);
                unset($one['order_items']);
            }

        }

        return $this->result($result);

    }

    /**
     * 对订单进行预处理
     * @param $order_items
     * @return array
     */
    private function preprocess($order_items){
        $process = [];

        foreach ($order_items as $item){
            $key = md5($item['uid'].$item['place_origin']);
            if(!isset($process[$key])){
                $process[$key]['items'] = [];
                $process[$key]['key'] = $key;
                $process[$key]['place_origin'] = $item['place_origin'];
                $process[$key]['contact_name'] = $item['contact_name'];
            }

            array_push($process[$key]['items'],$item);
        }
        $tmp = [];
        foreach ($process as $item){
            array_push($tmp,$item);
        }

        return $tmp;
    }

}